// PolicyCompute_v4.5.js

import pc4 from "./PolicyCompute_v4.js";
import dayjs from "dayjs";
export default function (cellData, step = 4, cnOnly = false) {
  if (!cellData || cellData.length == 0) return cellData;
  if (!cnOnly) cellData = pc4(cellData);
  for (let i = 0; i < cellData.length; i++) {
    let hour = dayjs(cellData[i].timestamp).hour();
    let seatAppending = 0;
    let flowYBLZ60 =
      cellData[i].airport.ZULZ.total + cellData[i].airport.ZUYB.total;

    let flowYBLZ120 =
      cellData[i].airport.ZULZ.total + cellData[i].airport.ZUYB.total;
    if (step == 4 && i < cellData.length - 1) {
      flowYBLZ120 +=
        cellData[i + 1].airport.ZULZ.total + cellData[i + 1].airport.ZUYB.total;
    } else if (step == 2 && i < cellData.length - 1) {
      flowYBLZ60 +=
        cellData[i + 1].airport.ZULZ.total + cellData[i + 1].airport.ZUYB.total;
    } else if (step == 1 && i < cellData.length - 3) {
      flowYBLZ60 +=
        cellData[i + 1].airport.ZULZ.total + cellData[i + 1].airport.ZUYB.total;
      flowYBLZ60 +=
        cellData[i + 2].airport.ZULZ.total + cellData[i + 2].airport.ZUYB.total;
      flowYBLZ60 +=
        cellData[i + 3].airport.ZULZ.total + cellData[i + 3].airport.ZUYB.total;
    }

    if (step == 2 && i < cellData.length - 3) {
      flowYBLZ120 +=
        cellData[i + 1].airport.ZULZ.total + cellData[i + 1].airport.ZUYB.total;
      flowYBLZ120 +=
        cellData[i + 2].airport.ZULZ.total +
        cellData[i + 2].airport.ZUYB.total +
        cellData[i + 3].airport.ZULZ.total +
        cellData[i + 3].airport.ZUYB.total;
    } else if (step == 1 && i < cellData.length - 7) {
      flowYBLZ120 +=
        cellData[i + 1].airport.ZULZ.total + cellData[i + 1].airport.ZUYB.total;
      flowYBLZ120 +=
        cellData[i + 2].airport.ZULZ.total +
        cellData[i + 2].airport.ZUYB.total +
        cellData[i + 3].airport.ZULZ.total +
        cellData[i + 3].airport.ZUYB.total;
      flowYBLZ120 +=
        cellData[i + 4].airport.ZULZ.total +
        cellData[i + 4].airport.ZUYB.total +
        cellData[i + 5].airport.ZULZ.total +
        cellData[i + 5].airport.ZUYB.total +
        cellData[i + 6].airport.ZULZ.total +
        cellData[i + 6].airport.ZUYB.total +
        cellData[i + 7].airport.ZULZ.total +
        cellData[i + 7].airport.ZUYB.total;
    }
    if (hour >= 8 && hour < 18) {
      seatAppending = 2;
    } else {
      if (flowYBLZ120 < 3) {
        seatAppending = 0;
      } else if (flowYBLZ60 <= 3) {
        seatAppending = 1;
      } else if (flowYBLZ60 > 3) {
        seatAppending = 2;
      }
    }
    cellData[i].sectorRequire = cellData[i].sectorRequire ?? 0;
    cellData[i].sectorReason =
      cellData[i].sectorReason ?? "" + ` 川南扇区${seatAppending / 2} `;
    cellData[i].seatAdvice = cellData[i].sectorRequire * 2;
    cellData[i].seatAdviceCn = seatAppending;
    cellData[i].sectorRequireCn = seatAppending / 2;
    // cellData[i].seatAdviceAll = cellData[i].seatAdvice + seatAppending;
    // @20241121: 删除了川南进近席位数增量
    cellData[i].seatAdviceAll = cellData[i].seatAdvice;
    // console.log(
    //   "seatAppending",
    //   hour + "时：",
    //   "终端席位数：",
    //   cellData[i].sectorRequire * 2,
    //   "川南席位数：",
    //   seatAppending,
    //   "60分钟架次：",
    //   flowYBLZ60,
    //   "120分钟架次",
    //   flowYBLZ120
    // );
  }

  return cellData;
}
